<?php

/**
 * This is the model class for table "MidTab".
 *
 * The followings are the available columns in table 'MidTab':
 * @property integer $MidIndex
 * @property integer $TrackingIndex
 * @property integer $PricingIndex
 * @property integer $ProductID
 * @property integer $ToLAQty
 * @property string $CBoxNumber
 * @property string $CreateTime
 * @property string $UpdateTime
 * @property string $Login
 * @property string $Modifier
 * @property string $ShippingStatus
 */
class MidTab extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return MidTab the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'MidTab';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('TrackingIndex, PricingIndex, ProductID, ToLAQty', 'required'),
            array('TrackingIndex, PricingIndex, ProductID, ToLAQty', 'numerical', 'integerOnly' => true),
            array('CBoxNumber, Login, Modifier, ShippingStatus', 'length', 'max' => 50),
            array('CreateTime, UpdateTime', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('MidIndex, TrackingIndex, PricingIndex, ProductID, ToLAQty, CBoxNumber, CreateTime, UpdateTime, Login, Modifier, ShippingStatus', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'product' => array(self::BELONGS_TO, 'Product', 'ProductID'),
            'pricing' => array(self::BELONGS_TO, 'Pricing', 'PricingIndex'),
            'tracking' => array(self::BELONGS_TO, 'Tracking', 'TrackingIndex'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'MidIndex' => 'Mid Index',
            'TrackingIndex' => 'Tracking Index',
            'PricingIndex' => 'Pricing Index',
            'ProductID' => 'Product',
            'ToLAQty' => 'To Laqty',
            'CBoxNumber' => '海运箱号',
            'CreateTime' => 'Create Time',
            'UpdateTime' => 'Update Time',
            'Login' => 'Login',
            'Modifier' => 'Modifier',
            'ShippingStatus' => 'Shipping Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('MidIndex', $this->MidIndex);
        $criteria->compare('TrackingIndex', $this->TrackingIndex);
        $criteria->compare('PricingIndex', $this->PricingIndex);
        $criteria->compare('ProductID', $this->ProductID);
        $criteria->compare('ToLAQty', $this->ToLAQty);
        $criteria->compare('CBoxNumber', $this->CBoxNumber, true);
        $criteria->compare('CreateTime', $this->CreateTime, true);
        $criteria->compare('UpdateTime', $this->UpdateTime, true);
        $criteria->compare('Login', $this->Login, true);
        $criteria->compare('Modifier', $this->Modifier, true);
        $criteria->compare('ShippingStatus', $this->ShippingStatus, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {
            $able = Helper::getAbleNumByPricingIndex($this->PricingIndex);
            $flag = true;
            if ($this->isNewRecord) {
                $this->Login = $this->Modifier = Yii::app()->user->name;
                $this->CreateTime = $this->UpdateTime = date('Y-m-d H:i:s');
                $this->ShippingStatus = 'DataLogging';
                if($this->ToLAQty>$able){
                    $flag = false;
                }
            }else{
                $this->UpdateTime = date('Y-m-d H:i:s');
                $this->Modifier = Yii::app()->user->name;
                if($this->ToLAQty>$able+Helper::getMidToLAQty($this->MidIndex)){
                    $flag = false;
                }
            }
        }
        return $flag;
    }
    
    protected function afterSave() {
        parent::afterSave();
        Helper::updateTrackingCacheByPricingIndex($this->PricingIndex);
    }
    
    protected function beforeDelete() {
        parent::beforeDelete();
        Yii::app()->user->setFlash('pid',  $this->PricingIndex);
        return true;
    }
    
    protected function afterDelete() {
        parent::afterDelete();
        $pid = Yii::app()->user->getFlash('pid');
        Helper::updateTrackingCacheByPricingIndex($pid);
    }

}